//
//  ViewController.m
//  UITableViewHomework
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "City.h"
#import "Distric.h"
#import "Province.h"
#import "SecondViewController.h"
#define kTableView @"resue"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic ,strong)NSMutableArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
      [super viewDidLoad];
      NSString *path = @"/Users/dllo/Desktop/New/UITableViewHomework/UITableViewHomework/area.txt";
      NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
      NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
      _arr = [NSMutableArray array];
      for (NSString *area in allArray) {
            if (![area hasPrefix:@" "]) {
                  Province *province = [[Province alloc]initWithName:area];
                  [_arr addObject:province];
                  
            }else if (![area hasPrefix:@"   "]) {
                  City *city = [[City alloc]initWithName:area];
                  Province *province  = [_arr lastObject];
                  [province.arr addObject:city];
            
            }else {
                  Distric *distric = [[Distric alloc]initWithName:area];
                  Province *province = [_arr lastObject];
                  City *city = [province.arr lastObject];
                  [city.arr addObject:distric];
            
            }
      }
      _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
      [self.view addSubview:_tableView];
      _tableView.delegate = self;
      _tableView.dataSource = self;
//      [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


      return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
      if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableView];
            
      }
      Province *province = _arr[indexPath.row];
      NSArray *arr = [province.name componentsSeparatedByString:@" "];
      cell.textLabel.text = arr.firstObject;
      cell.detailTextLabel.text = arr.lastObject;
      return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      SecondViewController *second = [[SecondViewController alloc]init];
      [self.navigationController pushViewController:second animated:YES];
      second.province = _arr[indexPath.row];

}
@end
